home *** CD-ROM | disk | FTP | other *** search
/ Aminet 22 / Aminet 22 (1997)(GTI - Schatztruhe)[!][Dec 1997].iso / Aminet / biz / dbase / db.lha / Examples / ARexxDemos / checkdate.db < prev    next >
Text File  |  1995-06-28  |  3KB  |  123 lines

  1. /* checkdate.db
  2.  * ARexx script for the database program db. V1.0 David Ekholm, 1995
  3.  * Convert dates to the dd-Mmm-YY format. This code is in the public domain!
  4.  * You may easily adapt it to your favourite date format.
  5.  *
  6.  * Input rules:
  7.  * Days, months and years may be delimited by either a - or a / or nothing
  8.  * Days are accepted as either d or dd.
  9.  * Months are accepted as either m mm or mmm.
  10.  * Partly completed (non numeric) months are expanded (ie j -> JAN)
  11.  * Years are accepted as either yy or yyyy if a delimiter is used.
  12.  * 
  13.  * Examples:
  14.  * 1195             1-Jan-95
  15.  * 10195             1-Jan-95
  16.  * 010195         1-Jan-95
  17.  * 15395            15-mar-95
  18.  * 1-jan-95         1-Jan-95
  19.  * 1-jan-1995     1-Jan-1995
  20.  * 29-2-95        error: February only has 28 days!
  21.  * 29-2-96        29-Feb-96  Ok, leap year!
  22.  * 29-f-96        29-Feb-96  Ok, leap year!
  23.  *
  24.  * If input doesn't conform to these rules, the screen will beep and you will have to try
  25.  * again
  26.  */
  27.  
  28. options results
  29.  
  30. GETFIELD
  31. indate = result
  32.  
  33. if indate ~= "" then do
  34.     outdate = convdate(indate)
  35.     if outdate == 'error' then do
  36.         DISPLAYBEEP
  37.         RETRYINPUT
  38.     end
  39.     else PUTFIELD outdate
  40. end
  41. exit
  42.  
  43.  
  44. convdate: procedure
  45.     arg indate    /* indate is now in UPPER case */
  46.  
  47.     months = "Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec"
  48.     lengths = "31 28  31  30  31  30  31  31  30  31  30  31"
  49.     leaps =   "31 29  31  30  31  30  31  31  30  31  30  31"
  50.     umonths = upper(months)
  51.     savedate = indate
  52.  
  53.     /* Change dd/mm/yy, dd-mm-yy or ddmmyy to dd mm yy */
  54.     if pos('/', indate) ~= 0 then
  55.         indate = translate(indate, ' ', '/')
  56.  
  57.     else if pos('-', indate) ~= 0 then
  58.         indate = translate(indate, ' ', '-')
  59.  
  60.     else do         /* No delimiter */
  61.         len = length(indate)
  62.         if len == 4 then do
  63.             indate = insert(' ', indate, 2)
  64.             indate = insert(' ', indate, 1)
  65.         end
  66.         else
  67.         if len == 5 then do
  68.             indate = insert(' ', indate, 3)
  69.             indate = insert(' ', indate, 1)
  70.  
  71.             /* fix month ambiguity (ie is '21393' 2-xxx-93 or 21-Mar-93 ?) */
  72.             month = word(indate,2)
  73.             if datatype(month, 'Whole') & month<1 | month>12 then do
  74.                 indate = insert(' ', savedate, 3)
  75.                 indate = insert(' ', indate, 2)
  76.             end
  77.         end
  78.         else if len == 6 then do
  79.             indate = insert(' ', indate, 4)
  80.             indate = insert(' ', indate, 2)
  81.         end
  82.         else return 'error'
  83.         
  84.     end
  85.  
  86.     /* Now parse the dd mm yy format */
  87.     if words(indate) ~= 3 then return 'error'
  88.     day = word(indate,1)
  89.     month = word(indate,2)
  90.     year = word(indate,3)
  91.     
  92.     /* fix year */
  93.     if ~datatype(year,'Whole') then return 'error'
  94.  
  95.     /* fix month */
  96.     if datatype(month,'Whole') then do
  97.         if month > 0 & month <= 12 then do
  98.             monthnum = month
  99.             month = word(months,month)
  100.         end
  101.         else return 'error'
  102.     end
  103.     else do
  104.         i = pos(month, umonths)
  105.         if i == 0 then return 'error'
  106.         monthnum = i % 4 + 1
  107.         month = subword(months, monthnum,1)        /* Expand eg j to JAN */
  108.     end
  109.  
  110.     /* fix day */
  111.     if datatype(day,'Whole') & day>0 then do
  112.         day = day + 0         /* Convert eg 01 to 1 */
  113.         if year // 4 == 0 then do     /* Leap year */
  114.             if day > subword(leaps, monthnum,1) then return 'error'
  115.         end
  116.         else if day > subword(lengths, monthnum,1) then return 'error'
  117.     end
  118.     else return 'error'
  119.  
  120.     /* Here we specify the returnformat. */
  121.     /* Use monthnum instead of month to get an all-numeric date */
  122.     return day || '-' || month || '-' || year
  123.